﻿<?php
class acc_mgt_cont extends CI_Controller {
    public function index()
    {
        //$this->output->cache(5);
		$is_logged_in = $this->session->userdata('logged_in');
		if(!isset($is_logged_in) || $is_logged_in != true)
		{
			$this->load->view('home');
		}
        else
		{
			$this->load();
		}
    }
	public function about()
    {
        //$this->output->cache(5);
        $this->load->view('about');
    }
	public function login()
    {
		
		$email=$this->input->post('email');
		$password=$this->input->post('password');
			
		$this->load->model('acc_mgt_mod');
		$l = $this->acc_mgt_mod->validate($email,$password);
		//session
		if($l)
		{
			$loggedin = array(
				'username' => $email,
				'logged_in' => true
			);
			$this->session->set_userdata($loggedin);
			
			$this->load();
			//end_session
		}
		else
		{
			$this->load->view('loginfailed');
		}
		
    }
	function load()
	{
		//////////////////////////////////get latest jobs/////////////////////////////////
			
			
			$this->db->select('j_id,j_duedate,j_req,j_place,j_desc,j_title');
			
			$this->db->order_by("j_modification_date", "desc"); 
			$records = $this->db->get('jobs');
			$matching_jobs=$this->matching_jobs();
			$user_tags=$this->get_tags();
			//save to arrays///////////////////////////
			$array_of_jobs=array();
			
			foreach ($records->result_array() as $row)
			{
				$j_id=$row['j_id'];
				$j_title=$row['j_title'];
				$j_desc=$row['j_desc'];
				$j_place=$row['j_place'];
				$j_duedate=$row['j_duedate'];
				
				$data = array(
					'j_id'=> $j_id,
					'j_title'=> $j_title,
					'j_desc'=> $j_desc,
					'j_place'=> $j_place,
					'j_duedate'=> $j_duedate
				);
				array_push($array_of_jobs,$data);
			}
			$job_json=json_encode($array_of_jobs);
			/////////////////////matching//////////////////
			$array_of_match=array();
			if($matching_jobs!='')
			{
				foreach ($matching_jobs->result_array() as $row)
				{
					$j_id=$row['j_id'];
					$j_title=$row['j_title'];
					$j_desc=$row['j_desc'];
					$j_place=$row['j_place'];
					$j_duedate=$row['j_duedate'];
					
					$data = array(
						'j_id'=> $j_id,
						'j_title'=> $j_title,
						'j_desc'=> $j_desc,
						'j_place'=> $j_place,
						'j_duedate'=> $j_duedate
					);
					array_push($array_of_match,$data);
				}
			}
			$match_json=json_encode($array_of_match);
			
			///////////////////////////////////////////
			$data['records']=$job_json;
			$data['matching_jobs']=$match_json;
			$data['records']=json_encode($data['records']);
			$data['matching_jobs']=json_encode($data['matching_jobs']);
			$data['user_tags']=json_encode($user_tags);
			//echo $data;
			//$data=json_encode($data);
			//////////////////////////////////get latest jobs////////////////////////////////////
			
			$this->load->view('home_user',$data);
	}
	public function logout()
    {
		$data = array('username' => '', 'logged_in' => '');
		$this->session->unset_userdata($data);
        $this->load->view('home');
    }
	public function register()
    {
		$email=$this->input->post('email');
		$password=$this->input->post('password');
		$tags=$this->input->post('tags');
		$this->load->model('acc_mgt_mod');
		$exist=$this->acc_mgt_mod->is_email_exist($email);
		if(!$exist)
		{
			$data=array
			(
				'u_email'=> $email,
				'u_password'=> md5($password),
			);
			$this->load->model('acc_mgt_mod');
			$this->acc_mgt_mod->create_user($data);
			//session
			$loggedin = array(
				'username' => $email,
				'logged_in' => true
			);
			$this->session->set_userdata($loggedin);
			//session_end
			$this->load->view('registersuccess');
		}
		else
		{
			$this->load->view('registerfailed');
		}
		
    }
	public function search_job()
	{
		//////////////////////////////////get job////////////////////////////////////
		if(!isset($_POST['keyword']))
		{$_POST['keyword']='';}
		$keyword=$_POST['keyword'];
		
		$this->db->select('j_id,j_duedate,j_req,j_place,j_desc,j_title');
		$k=explode(' ',$keyword);
		$this->db->like('j_title',$keyword);
		$this->db->or_like('j_desc',$keyword);
		$this->db->or_like('j_place',$keyword);
		$this->db->or_like('j_req',$keyword);
		foreach($k as $word)
		{
			$this->db->or_like('j_title',$word);
			$this->db->or_like('j_desc',$word);
			$this->db->or_like('j_place',$word);
			$this->db->or_like('j_req',$word);
		}
		$this->db->order_by("j_modification_date", "desc"); 
		$q= $this->db->get('jobs');
		
			$array_of_match=array();
			foreach ($q->result_array() as $row)
			{
				$j_id=$row['j_id'];
				$j_title=$row['j_title'];
				$j_desc=$row['j_desc'];
				$j_place=$row['j_place'];
				$j_duedate=$row['j_duedate'];
				
				$data = array(
					'j_id'=> $j_id,
					'j_title'=> $j_title,
					'j_desc'=> $j_desc,
					'j_place'=> $j_place,
					'j_duedate'=> $j_duedate
				);
				array_push($array_of_match,$data);
			}
			$match_json=json_encode($array_of_match);
			echo $match_json;
	
		//////////////////////////////////get job////////////////////////////////////
		
	}
	function matching_jobs()
	{	
		$this->load->model('acc_mgt_mod');
		$res = $this->acc_mgt_mod->getId($this->session->userdata('username'));
		foreach($res->result_array()as $row)
		{
			$u_id=$row['u_id'];
		}
		$this->load->model('tags_mod');
		$l = $this->tags_mod->getTags($u_id);
		$this->db->select('j_id,j_duedate,j_req,j_place,j_desc,j_title');
		foreach($l->result_array()as $row)
		{
			$this->db->or_like('j_title',$row['t_name']);
			$this->db->or_like('j_desc',$row['t_name']);
			$this->db->or_like('j_place',$row['t_name']);
			$this->db->or_like('j_req',$row['t_name']);
		}
		//////////////////////////////////get matching jobs/////////////////////////////////
		$this->db->order_by("j_modification_date", "desc"); 
		$data['matching_jobs'] = $this->db->get('jobs');
		if($l->num_rows()!=0)
			return $data['matching_jobs'];
		else 
			return '';
		//////////////////////////////////get matching jobs////////////////////////////////////
	}
	function add_tags()
	{
		$this->load->model('acc_mgt_mod');
		$tags=$this->input->post('tags_user');
		$k=explode(',',$tags);
		$q= $this->acc_mgt_mod->getId($this->session->userdata('username'));
		foreach($q->result_array()as $row)
		{
			$owner_id=$row['u_id'];
		}
		foreach($k as $tag)
		{
			$data=array(
				't_name'=>$tag,
				'u_id'=>$owner_id
			);
			$this->acc_mgt_mod->add_tags($data);
		}
		$this->load();
	}
	function get_tags()
	{
		$this->load->model('acc_mgt_mod');
		$tags=$this->input->post('tags_user');
		$k=explode(',',$tags);
		$q= $this->acc_mgt_mod->getId($this->session->userdata('username'));
		foreach($q->result_array()as $row)
		{
			$owner_id=$row['u_id'];
		}
		$q=$this->acc_mgt_mod->get_tags($owner_id);
		$array_of_match=array();
		foreach($q->result_array()as $row)
		{
			$data=$row['t_name'].'###'.$row['t_id'];
			array_push($array_of_match,$data);
		}
		return $array_of_match;
		
	}
	function del_tag($t_id)
	{
		$this->load->model('tags_mod');
		$this->tags_mod->delete($t_id);
		$this->load();
	}
	function chang_pass()
	{
		$old_pass=$this->input->post('old_pass');
		$new_pass=$this->input->post('new_pass');
		$re_new_pass=$this->input->post('re_new_pass');
		$this->load->model('acc_mgt_mod');
		$email=$this->session->userdata('username');
		$result=$this->acc_mgt_mod->validate($email,$old_pass);
		if($result)
		{
			if($new_pass==$re_new_pass)
			{
				$this->acc_mgt_mod->change_pass($email,$new_pass);
				$this->load();
			}
			else
				$this->load->view('wrong_pass');
		}
		else
		{
			$this->load->view('wrong_pass');
		}
		
	}
	
}